package sdk.pay;

import android.app.Activity;

import java.util.List;

import sdk.pay.listener.Pay;
import sdk.pay.listener.PayGetPayStatusListener;
import sdk.pay.listener.PayGetPayTypeListener;
import sdk.pay.listener.PayUtilCallBack;
import sdk.pay.model.PayTypeModel;
import sdk.pay.model.TokenParam;

import static sdk.pay.constant.PayExceptionType.INIT_PARAMS_ERROR;

public class PayUtil implements Pay {
    private PayHelper mPayHelper;

    public PayUtil(Activity activity, PayUtilCallBack callback) {
        this(activity, callback, false);
    }

    public PayUtil(Activity activity, PayUtilCallBack callback, boolean payStatusCallBack) {
        this(activity, null, null, null, null, null, callback, payStatusCallBack);
    }

    public PayUtil(Activity activity, String appid, String key, String vector, String systemName, String code, PayUtilCallBack callback, boolean payStatusCallBack) {
        mPayHelper = new PayHelperIml(activity, appid, key, vector, systemName, code, callback, payStatusCallBack);
    }

    @Override
    public void getToken(TokenParam tokenParam, PayGetPayTypeListener getPayTypeListener) {
        if (mPayHelper.isValidInitParams())
            mPayHelper.getToken(tokenParam, getPayTypeListener);
        else
            mPayHelper.showToast(INIT_PARAMS_ERROR.getMessage());
    }

    /**
     * @param tokenParam TokenParam
     * @param payType    int
     */
    @Override
    public void pay(TokenParam tokenParam, int payType) {
        if (mPayHelper.isValidInitParams())
            mPayHelper.pay(tokenParam, payType);
        else
            mPayHelper.showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public void getToken(TokenParam tokenParam, int payType) {
        pay(tokenParam, payType);
    }

    @Override
    public void setPayParam(String... payParam) {
        mPayHelper.setPayParam(payParam);
    }

    @Override
    public void getPayTypes(PayGetPayTypeListener getPayTypeListener) {
        if (mPayHelper.isValidInitParams())
            mPayHelper.getPayTypes(getPayTypeListener);
        else
            mPayHelper.showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public List<PayTypeModel> getPayTypeModels() {
        if (mPayHelper.isValidInitParams()) {
            return mPayHelper.getPayTypeModels();
        } else {
            mPayHelper.showToast(INIT_PARAMS_ERROR.getMessage());
        }
        return null;
    }

    @Override
    public void getPayParam(int typeId) {
        if (mPayHelper.isValidInitParams())
            mPayHelper.getPayParam(typeId);
        else
            mPayHelper.showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public void getPayStatus(PayGetPayStatusListener getPayStatusListener) {
        mPayHelper.getPayStatus(getPayStatusListener);
    }

    @Override
    public void destroy() {
        mPayHelper.destroy();
    }
}
